package org.dromara.datacheck.service.impl.jd;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.datacheck.domain.bo.jd.JdSaleOutWarehouseOrderBo;
import org.dromara.datacheck.domain.jd.JdSaleOutWarehouseOrder;
import org.dromara.datacheck.domain.vo.jd.JdSaleOutWarehouseOrderVo;
import org.dromara.datacheck.mapper.jd.JdSaleOutWarehouseOrderMapper;
import org.dromara.datacheck.service.jd.IJdSaleOutWarehouseOrderService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 金蝶-销售出库单Service业务层处理
 *
 * @author 刘武贵
 * @date 2024-09-08
 */
@RequiredArgsConstructor
@Service
public class JdSaleOutWarehouseOrderServiceImpl implements IJdSaleOutWarehouseOrderService {

    private final JdSaleOutWarehouseOrderMapper baseMapper;

    /**
     * 查询金蝶-销售出库单
     *
     * @param id 主键
     * @return 金蝶-销售出库单
     */
    @Override
    public JdSaleOutWarehouseOrderVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询金蝶-销售出库单列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 金蝶-销售出库单分页列表
     */
    @Override
    public TableDataInfo<JdSaleOutWarehouseOrderVo> queryPageList(JdSaleOutWarehouseOrderBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<JdSaleOutWarehouseOrder> lqw = buildQueryWrapper(bo);
        Page<JdSaleOutWarehouseOrderVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的金蝶-销售出库单列表
     *
     * @param bo 查询条件
     * @return 金蝶-销售出库单列表
     */
    @Override
    public List<JdSaleOutWarehouseOrderVo> queryList(JdSaleOutWarehouseOrderBo bo) {
        LambdaQueryWrapper<JdSaleOutWarehouseOrder> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    /**
     * 构建查询包装器
     *
     * @param bo 业务对象，包含查询条件和参数
     * @return LambdaQueryWrapper 查询包装器，用于后续的条件查询
     */
    private LambdaQueryWrapper<JdSaleOutWarehouseOrder> buildQueryWrapper(JdSaleOutWarehouseOrderBo bo) {
        // 获取业务对象中的参数
        Map<String, Object> params = bo.getParams();

        // 初始化LambdaQueryWrapper，用于组装查询条件
        LambdaQueryWrapper<JdSaleOutWarehouseOrder> lqw = Wrappers.lambdaQuery();

        // 拼接查询条件，只有当参数不为空时才添加到查询条件中
        lqw.eq(StringUtils.isNotBlank(bo.getSaleOutWarehouseOrderDate()), JdSaleOutWarehouseOrder::getSaleOutWarehouseOrderDate, bo.getSaleOutWarehouseOrderDate());
        lqw.eq(StringUtils.isNotBlank(bo.getSaleOutWarehouseOrderNumber()), JdSaleOutWarehouseOrder::getSaleOutWarehouseOrderNumber, bo.getSaleOutWarehouseOrderNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getSaleOutWarehouseOrderCustomer()), JdSaleOutWarehouseOrder::getSaleOutWarehouseOrderCustomer, bo.getSaleOutWarehouseOrderCustomer());
        lqw.eq(StringUtils.isNotBlank(bo.getDeliverGoodsOrganization()), JdSaleOutWarehouseOrder::getDeliverGoodsOrganization, bo.getDeliverGoodsOrganization());
        lqw.eq(StringUtils.isNotBlank(bo.getSaleOutWarehouseOrderStatus()), JdSaleOutWarehouseOrder::getSaleOutWarehouseOrderStatus, bo.getSaleOutWarehouseOrderStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getMaterialCode()), JdSaleOutWarehouseOrder::getMaterialCode, bo.getMaterialCode());
        lqw.like(StringUtils.isNotBlank(bo.getMaterialName()), JdSaleOutWarehouseOrder::getMaterialName, bo.getMaterialName());
        lqw.like(StringUtils.isNotBlank(bo.getInventoryUnitName()), JdSaleOutWarehouseOrder::getInventoryUnitName, bo.getInventoryUnitName());
        lqw.eq(bo.getActualDeliveryNumber() != null, JdSaleOutWarehouseOrder::getActualDeliveryNumber, bo.getActualDeliveryNumber());
        lqw.like(StringUtils.isNotBlank(bo.getWarehouseName()), JdSaleOutWarehouseOrder::getWarehouseName, bo.getWarehouseName());
        lqw.eq(StringUtils.isNotBlank(bo.getSaleOutWarehouseOrderRemark()), JdSaleOutWarehouseOrder::getSaleOutWarehouseOrderRemark, bo.getSaleOutWarehouseOrderRemark());
        if (params.get("beginTime") != null && params.get("endTime") != null) {
            lqw.between(JdSaleOutWarehouseOrder::getNewOrderDate,
                params.get("beginTime"),
                params.get("endTime"));
        }
        // 返回组装好的查询包装器
        return lqw;
    }


    /**
     * 新增金蝶-销售出库单
     *
     * @param bo 金蝶-销售出库单
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(JdSaleOutWarehouseOrderBo bo) {
        JdSaleOutWarehouseOrder add = MapstructUtils.convert(bo, JdSaleOutWarehouseOrder.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改金蝶-销售出库单
     *
     * @param bo 金蝶-销售出库单
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(JdSaleOutWarehouseOrderBo bo) {
        JdSaleOutWarehouseOrder update = MapstructUtils.convert(bo, JdSaleOutWarehouseOrder.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(JdSaleOutWarehouseOrder entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除金蝶-销售出库单信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }

    /**
     * 查询金蝶-销售出库单列表所有数据
     *
     * @return 金蝶-销售出库单列表
     */
    @Override
    public List<JdSaleOutWarehouseOrder> queryAll() {
        return baseMapper.selectList(null);
    }

    /**
     * 清空金蝶-销售出库单列表所有数据
     *
     * @return 金蝶-销售出库单列表
     */
    @Override
    public Integer deleteAll(){
        return baseMapper.delete(new QueryWrapper<>());
    }

    /**
     * 查询金蝶-销售出库单列表
     * 场景分类：寄售-寄寄售换货-发货(猫超售后店铺，进口猫超售后店铺)
     * 业务查询条件：根据sale_out_warhouse_order_customer客户为猫超寄售售后、进口超市售后店铺查询
     *
     * @param bo
     * @param pageQuery
     */
    @Override
    public TableDataInfo<JdSaleOutWarehouseOrderVo> queryPageListByCustomer1(JdSaleOutWarehouseOrderBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<JdSaleOutWarehouseOrder> lqw = buildQueryWrapperByCustomer1(bo);
        Page<JdSaleOutWarehouseOrderVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }


    /**
     * 构建查询包装器
     * 场景分类：寄售-寄寄售换货-发货(猫超售后店铺，进口猫超售后店铺)
     * 业务查询条件：根据sale_out_warhouse_order_customer客户为猫超寄售售后、进口超市售后店铺查询
     *
     * @param bo 业务对象，包含查询条件和参数
     * @return LambdaQueryWrapper 查询包装器，用于后续的条件查询
     */
    private LambdaQueryWrapper<JdSaleOutWarehouseOrder> buildQueryWrapperByCustomer1(JdSaleOutWarehouseOrderBo bo) {
        // 获取业务对象中的参数
        Map<String, Object> params = bo.getParams();

        // 初始化LambdaQueryWrapper，用于组装查询条件
        LambdaQueryWrapper<JdSaleOutWarehouseOrder> lqw = Wrappers.lambdaQuery();

        // 拼接查询条件，只有当参数不为空时才添加到查询条件中
        lqw.eq(StringUtils.isNotBlank(bo.getSaleOutWarehouseOrderDate()), JdSaleOutWarehouseOrder::getSaleOutWarehouseOrderDate, bo.getSaleOutWarehouseOrderDate());
        lqw.eq(StringUtils.isNotBlank(bo.getSaleOutWarehouseOrderNumber()), JdSaleOutWarehouseOrder::getSaleOutWarehouseOrderNumber, bo.getSaleOutWarehouseOrderNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getSaleOutWarehouseOrderCustomer()), JdSaleOutWarehouseOrder::getSaleOutWarehouseOrderCustomer, bo.getSaleOutWarehouseOrderCustomer());
        lqw.eq(StringUtils.isNotBlank(bo.getDeliverGoodsOrganization()), JdSaleOutWarehouseOrder::getDeliverGoodsOrganization, bo.getDeliverGoodsOrganization());
        lqw.eq(StringUtils.isNotBlank(bo.getSaleOutWarehouseOrderStatus()), JdSaleOutWarehouseOrder::getSaleOutWarehouseOrderStatus, bo.getSaleOutWarehouseOrderStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getMaterialCode()), JdSaleOutWarehouseOrder::getMaterialCode, bo.getMaterialCode());
        lqw.like(StringUtils.isNotBlank(bo.getMaterialName()), JdSaleOutWarehouseOrder::getMaterialName, bo.getMaterialName());
        lqw.like(StringUtils.isNotBlank(bo.getInventoryUnitName()), JdSaleOutWarehouseOrder::getInventoryUnitName, bo.getInventoryUnitName());
        lqw.eq(bo.getActualDeliveryNumber() != null, JdSaleOutWarehouseOrder::getActualDeliveryNumber, bo.getActualDeliveryNumber());
        lqw.like(StringUtils.isNotBlank(bo.getWarehouseName()), JdSaleOutWarehouseOrder::getWarehouseName, bo.getWarehouseName());
        lqw.eq(StringUtils.isNotBlank(bo.getSaleOutWarehouseOrderRemark()), JdSaleOutWarehouseOrder::getSaleOutWarehouseOrderRemark, bo.getSaleOutWarehouseOrderRemark());
        // 客户
        lqw.in(CollUtil.isNotEmpty(bo.getSaleOutWarhouseOrderCustomerList()), JdSaleOutWarehouseOrder::getSaleOutWarehouseOrderCustomer, bo.getSaleOutWarhouseOrderCustomerList());
        if (params.get("beginTime") != null && params.get("endTime") != null) {
            lqw.between(JdSaleOutWarehouseOrder::getNewOrderDate,
                params.get("beginTime"),
                params.get("endTime"));
        }
        lqw.eq(StringUtils.isNotBlank(bo.getSaleOutWarehouseOrderStatus()), JdSaleOutWarehouseOrder::getSaleOutWarehouseOrderStatus, "已审核");
        // 返回组装好的查询包装器
        return lqw;
    }

    /**
     * 查询金蝶-销售出库单列表
     * 场景分类：分销（山姆）销售-销售
     * 业务查询条件：根据sale_out_warhouse_order_customer客户为山姆会员店查询
     *
     * @param bo
     * @param pageQuery
     */
    @Override
    public TableDataInfo<JdSaleOutWarehouseOrderVo> queryPageListByCustomer2(JdSaleOutWarehouseOrderBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<JdSaleOutWarehouseOrder> lqw = buildQueryWrapperByCustomer2(bo);
        Page<JdSaleOutWarehouseOrderVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }


    /**
     * 构建查询包装器
     * 场景分类：分销（山姆）销售-销售
     * 业务查询条件：根据sale_out_warhouse_order_customer客户为山姆会员店查询
     *
     * @param bo 业务对象，包含查询条件和参数
     * @return LambdaQueryWrapper 查询包装器，用于后续的条件查询
     */
    private LambdaQueryWrapper<JdSaleOutWarehouseOrder> buildQueryWrapperByCustomer2(JdSaleOutWarehouseOrderBo bo) {
        // 获取业务对象中的参数
        Map<String, Object> params = bo.getParams();

        // 初始化LambdaQueryWrapper，用于组装查询条件
        LambdaQueryWrapper<JdSaleOutWarehouseOrder> lqw = Wrappers.lambdaQuery();

        // 拼接查询条件，只有当参数不为空时才添加到查询条件中
        lqw.eq(StringUtils.isNotBlank(bo.getSaleOutWarehouseOrderDate()), JdSaleOutWarehouseOrder::getSaleOutWarehouseOrderDate, bo.getSaleOutWarehouseOrderDate());
        lqw.eq(StringUtils.isNotBlank(bo.getSaleOutWarehouseOrderNumber()), JdSaleOutWarehouseOrder::getSaleOutWarehouseOrderNumber, bo.getSaleOutWarehouseOrderNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getSaleOutWarehouseOrderCustomer()), JdSaleOutWarehouseOrder::getSaleOutWarehouseOrderCustomer, bo.getSaleOutWarehouseOrderCustomer());
        lqw.eq(StringUtils.isNotBlank(bo.getDeliverGoodsOrganization()), JdSaleOutWarehouseOrder::getDeliverGoodsOrganization, bo.getDeliverGoodsOrganization());
        lqw.eq(StringUtils.isNotBlank(bo.getSaleOutWarehouseOrderStatus()), JdSaleOutWarehouseOrder::getSaleOutWarehouseOrderStatus, bo.getSaleOutWarehouseOrderStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getMaterialCode()), JdSaleOutWarehouseOrder::getMaterialCode, bo.getMaterialCode());
        lqw.like(StringUtils.isNotBlank(bo.getMaterialName()), JdSaleOutWarehouseOrder::getMaterialName, bo.getMaterialName());
        lqw.like(StringUtils.isNotBlank(bo.getInventoryUnitName()), JdSaleOutWarehouseOrder::getInventoryUnitName, bo.getInventoryUnitName());
        lqw.eq(bo.getActualDeliveryNumber() != null, JdSaleOutWarehouseOrder::getActualDeliveryNumber, bo.getActualDeliveryNumber());
        lqw.like(StringUtils.isNotBlank(bo.getWarehouseName()), JdSaleOutWarehouseOrder::getWarehouseName, bo.getWarehouseName());
        lqw.eq(StringUtils.isNotBlank(bo.getSaleOutWarehouseOrderRemark()), JdSaleOutWarehouseOrder::getSaleOutWarehouseOrderRemark, bo.getSaleOutWarehouseOrderRemark());
        // 客户
        lqw.in(CollUtil.isNotEmpty(bo.getSaleOutWarhouseOrderCustomerList()), JdSaleOutWarehouseOrder::getSaleOutWarehouseOrderCustomer, bo.getSaleOutWarhouseOrderCustomerList());
        if (params.get("beginTime") != null && params.get("endTime") != null) {
            lqw.between(JdSaleOutWarehouseOrder::getNewOrderDate,
                params.get("beginTime"),
                params.get("endTime"));
        }
        lqw.eq(StringUtils.isNotBlank(bo.getSaleOutWarehouseOrderStatus()), JdSaleOutWarehouseOrder::getSaleOutWarehouseOrderStatus, "已审核");
        // 返回组装好的查询包装器
        return lqw;
    }




}
